package com.zzz.utils;

import com.zzz.controller.User;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author zhuzhizun
 * @date 2021/8/13
 */
@Component
public class SpringUtils implements ApplicationContextAware, BeanFactoryPostProcessor {

    private static ApplicationContext applicationContext;
    private static ConfigurableListableBeanFactory factory;




    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println("SpringUtils-configurableListableBeanFactory 正在实例化");
        SpringUtils.factory = configurableListableBeanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        System.out.println("SpringUtils-applicationContext 正在实例化");
        SpringUtils.applicationContext = applicationContext;

    }
    public static Object getObject(String name) {
        Object object = null;
        object = applicationContext.getBean(name);

        return object;
    }

    public static BeanDefinition getBeanDefine(String name) {
        System.out.println("factory------------"+factory.getBean(name));
        BeanDefinition beanDefinition = factory.getBeanDefinition(name);
        beanDefinition.setAttribute("age",21);
        return beanDefinition;
    }

    public static String[] getActiveProfiles(){



        return applicationContext.getEnvironment().getActiveProfiles();

    }


}
